<?php

namespace app\admin\controller;

use think\facade\Db;

class NewsType extends Base
{
    public function index()
    {
        if (IS_POST) {
            $search = request()->param();
            $where = [];
            if (isset($search["name"]) && $search["name"] != "") $where[] = ["name", "=", $search["name"]];
            $db = Db::name('news_type')->where($where)->order('id desc');
            $list = $db->page($search['page'], $search['limit'])->select()->toArray();
            if (count($list)) {
                foreach ($list as &$v) {
                    $v['status'] = table_switch('NewsType/is_show', ['id' => $v['id']], $v['status'] == 1 ? true : false, '是|否');
                    $v['time'] = date('Y-m-d H:i:s', $v['time']);
                    $v['up_time'] = date('Y-m-d H:i:s', $v['up_time']);
                }
            }

            admin_response(0, 'success', $list, $db->count());
        }
        return $this->render_table([
            "page" => true,
            "toolbar" => [table_button('admin/NewsType/add', "添加", "frame", '', ['850px', '600px'])],
            "toolbar_row" => [
                table_button('admin/NewsType/edit', "编辑", "frame", '', ['850px', '600px']),
                table_button('admin/NewsType/del', "删除", "confirm")
            ],
            "cols" => [
                ["type" => "checkbox"],
                ["field" => "id", "title" => "ID", "align" => "center"],
                ["field" => "name", "title" => "类型名称", "align" => "center"],
                ["field" => "sort", "title" => "排序", "align" => "center"],
                ["field" => "status", "title" => "是否显示", "align" => "center"],
                ["field" => "time", "title" => "添加时间", "align" => "center"],
                ["field" => "up_time", "title" => "更新时间", "align" => "center"],

            ],
            "where" => [
                "name" => ["type" => "input", "title" => "类型名称"],
            ]

        ], false);

    }

    public function is_show()
    {
        $id = param_check('id');
        $checked = param_check('checked', 0);
        $res = Db::name('news_type')->where('id', $id)->update([
            'status' => $checked ? 1 : 0
        ]);
        $res ? json_response(1, '修改成功') : json_response(0, '修改失败');
    }

    public function add()
    {
        if (request()->isPost()) {
            $data = request()->post();
            $data['time'] = time();
            $data['up_time'] = time();
            if (Db::name('news_type')->insert($data)) json_response(1, '操作成功');
            json_response(0, '操作失败');
        } else {
            return $this->render_form([
                ["type" => "text", "name" => "name", "title" => "类型名称", "lay-verify" => "required"],
                ["type"=>"text","name"=>"sort","title"=>"排序","lay-verify"=>""],
                ['type' => 'radio', 'name' => 'status', 'title' => '状态', 'option' => [
                    ['title' => '显示', 'value' => 1],
                    ['title' => '隐藏', 'value' => 0]
                ]],
            ]);
        }
    }

    public function edit()
    {
        $id = \param_check('id');
        if (request()->isPost()) {
            $data = request()->post();
            $data['up_time'] = time();
            if (Db::name('news_type')->where('id', '=', $id)->update($data)) json_response(1, '操作成功');
            json_response(0, '操作失败');
        } else {
            $data = Db::name('news_type')->where('id', '=', $id)->find();
            return $this->render_form([
                ["type" => "text", "name" => "name", "title" => "名称", "lay-verify" => "required"],
                ["type"=>"text","name"=>"sort","title"=>"排序","lay-verify"=>""],
                ['type' => 'radio', 'name' => 'status', 'title' => '状态', 'option' => [
                    ['title' => '显示', 'value' => 1],
                    ['title' => '隐藏', 'value' => 0]
                ]],
            ], $data);
        }
    }


    public function del()
    {
        $id = param_check('id');
        if (!$banner = Db::name('news_type')->where('id', '=', $id)->find()) json_response(0, '不存在');
        if ($banner->delete()) json_response(1, '操作成功');
        json_response(0, '操作失败');
    }


}